package org.papervision3d.core.proto
{
    import org.papervision3d.materials.MaterialsList;
    
    import flash.events.EventDispatcher;
    import flash.utils.Dictionary;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.objects.DisplayObject3D;
    import flash.events.Event;
    
    // The DisplayObjectContainer3D class is the base class for all objects that can serve as DisplayObject3D containers.
    // Each DisplayObjectContainer3D object has its own child list.
    public class DisplayObjectContainer3D extends EventDispatcher
    {
        // Returns the number of children of this object.
        public function get numChildren():int
        {
            return this._childrenTotal;
        }
        
        /**
        * Add by pil
        * 
        * allow or prevent to dispatch MouseEvent3D
        */
        
        private var _enabled : Boolean = true;
        
        public function set enabled ( flag : Boolean ) : void {
        	_enabled = flag;
        	for each (var child:DisplayObject3D in this._childrenByName)
				child.enabled = flag;
        }
        
        public function get enabled () : Boolean {
        	return _enabled;
        }
        
        
    
        // Creates a new DisplayObjectContainer3D object.
        public function DisplayObjectContainer3D():void
        {
            this._children       = new Dictionary(false);
            this._childrenByName = new Dictionary(true);
            this._childrenTotal  = 0;
        }
    
        // Adds a child DisplayObject3D instance to this DisplayObjectContainer instance.
        // [TODO: If you add a child object that already has a different display object container as a parent, the object is removed from the child list of the other display object container.]
        // @param child The DisplayObject3D instance to add as a child of this DisplayObjectContainer3D instance.
        // @param name An optional name of the child to add or create. If no name is provided, the child name will be used.
        // @return The DisplayObject3D instance that you have added or created.
        public function addChild(child:DisplayObject3D, name:String = null):DisplayObject3D
        {
            // Choose name
            name = name || child.name || String(child.id);
    
            this._children[child] = name;
            this._childrenByName[name] = child;
            this._childrenTotal++;
            
            child.parent = this;
            child.dispatchEvent( new Event( Event.ADDED ) );
    
            return child;
        }
    
        // Removes the specified child DisplayObject3D instance from the child list of the DisplayObjectContainer3D instance.
        // [TODO: The parent property of the removed child is set to null, and the object is garbage collected if no other references to the child exist.]
        // The garbage collector is the process by which Flash Player reallocates unused memory space. When a variable or object is no longer actively referenced or stored somewhere, the garbage collector sweeps through and wipes out the memory space it used to occupy if no other references to it exist.
        // @param child The DisplayObject3D instance to remove.
        // @return The DisplayObject3D instance that you pass in the child parameter.
        public function removeChild(child:DisplayObject3D):DisplayObject3D
        {
            delete this._childrenByName[this._children[child]];
            delete this._children[child];
    
            child.parent = null;
            child.dispatchEvent( new Event( Event.REMOVED ) );
            
            return child;
        }
    
    
        // Returns the child display object that exists with the specified name.
        // If more that one child display object has the specified name, the method returns the first object in the child list.
        // @param    name    The name of the child to return.
        // @return   The child display object with the specified name.
        public function getChildByName(name:String):DisplayObject3D
        {
            return this._childrenByName[name];
        }
    
        // Removes the child DisplayObject3D instance that exists with the specified name, from the child list of the DisplayObjectContainer3D instance.
        // If more that one child display object has the specified name, the method removes the first object in the child list.
        // [TODO: The parent property of the removed child is set to null, and the object is garbage collected if no other references to the child exist.]
        // The garbage collector is the process by which Flash Player reallocates unused memory space. When a variable or object is no longer actively referenced or stored somewhere, the garbage collector sweeps through and wipes out the memory space it used to occupy if no other references to it exist.
        // @param name The name of the child to remove.
        // @return The DisplayObject3D instance that was removed.
        public function removeChildByName(name:String):DisplayObject3D
        {
            return removeChild(getChildByName(name));
        }
    
        // Returns a string value with the list of objects.
        // @return A string.
        public override function toString():String
        {
            return childrenList();
        }
    
        // Returns a string value with the list of objects.
        // @return A string.
        public function childrenList():String
        {
            var list:String = "";
    
            for (var name:String in this._children)
                list += name + "\n";
    
            return list;
        }
    
        // [internal-use] Names indexed by children.
        protected var _children:Dictionary;
    
        // [internal-use] Children indexed by name.
        public var _childrenByName:Dictionary;
    
        private var _childrenTotal:int;
    }
}
